package config

import (
	"cloud_worm/util/uconfig"
	"fmt"
)

type MysqlConfig struct {
	UserName 	string		`json:"user_name"`
	Password 	string		`json:"password"`
	Host 		string		`json:"host"`
	Port 		string		`json:"port"`
	Database 	string		`json:"database"`
}

func (m *MysqlConfig) parse(container uconfig.ConfContainer) error {
	runMode := container.String("run_mode")
	if runMode == "" {
		return fmt.Errorf("no run_mode exist")
	}
	runModePrefix := runMode + "::"

	m.UserName = container.String(runModePrefix + "mysql.user_name")
	m.Password = container.String(runModePrefix + "mysql.password")
	m.Host = container.String(runModePrefix + "mysql.host")
	m.Port = container.String(runModePrefix + "mysql.port")
	m.Database = container.String(runModePrefix + "mysql.database")

	if m.UserName == "" || m.Password == "" || m.Host == "" || m.Port == "" || m.Database == "" {
		return fmt.Errorf("some database config error")
	}
	return nil
}

type LocalConfig struct {
	PprofEnable 	bool			`json:"pprof_enable"`
	PprofPort		string			`json:"pprof_port"`
}

func (l *LocalConfig) parse(container uconfig.ConfContainer) error {
	runMode := container.String("run_mode")
	if runMode == "" {
		return fmt.Errorf("no run_mode exist")
	}
	runModePrefix := runMode + "::"

	pprofFlag, err := container.Bool(runModePrefix + "local.pprof_enable")
	if err != nil {
		return err
	}
	l.PprofEnable = pprofFlag

	l.PprofPort = container.String(runModePrefix + "local.pprof_port")
	if l.PprofPort == "" {
		return fmt.Errorf("config 'local.pprof_port' error")
	}

	return nil
}

type TLSCertInfo struct {
	PemFile 		string			`json:"pem_file"`
	KeyFile 		string			`json:"key_file"`
}

func (t *TLSCertInfo) parse(container uconfig.ConfContainer) error {
	runMode := container.String("run_mode")
	if runMode == "" {
		return fmt.Errorf("no run_mode exist")
	}
	runModePrefix := runMode + "::"

	t.PemFile = container.String(runModePrefix + "tls.pem_file")
	t.KeyFile = container.String(runModePrefix + "tls.key_file")
	if (t.PemFile == "" && t.KeyFile == "") || (t.PemFile != "" && t.KeyFile != "") {
		// nothing
	} else {
		return fmt.Errorf("some tls config error")
	}

	return nil
}

type Server struct {
	AccessPort 		string			`json:"access_port"`
	RpcPort 		string			`json:"rpc_port"`
	Domain 			string			`json:"domain"`
	
	IdcNumber 		int 			`json:"idc_number"`
}

func (s *Server) parse(container uconfig.ConfContainer) error {
	runMode := container.String("run_mode")
	if runMode == "" {
		return fmt.Errorf("no run_mode exist")
	}
	runModePrefix := runMode + "::"

	s.AccessPort = container.String(runModePrefix + "server.access_port")
	s.RpcPort = container.String(runModePrefix + "server.rpc_port")
	if s.AccessPort == "" || s.RpcPort == "" {
		return fmt.Errorf("some server config error")
	}
	s.Domain = container.String(runModePrefix + "server.domain")

	idc, err := container.Int(runModePrefix + "server.idc_number")
	if err != nil {
		return err
	}
	s.IdcNumber = idc

	return nil
}

type Remote struct {
	WebSrv 		string			`json:"web_srv"`
}

func (r *Remote) parse(container uconfig.ConfContainer) error {
	runMode := container.String("run_mode")
	if runMode == "" {
		return fmt.Errorf("no run_mode exist")
	}
	runModePrefix := runMode + "::"

	r.WebSrv = container.String(runModePrefix + "remote.web_srv")
	if r.WebSrv == "" {
		return fmt.Errorf("web_srv config error")
	}

	return nil
}

